<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * CLASS - JOURNALS
 * 
 * @name      journals
 * @package		OS CRM
 * @category  Class
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------

class journals extends Controller {
  
  /*common class varibale*/
  var $c 	= 'journals';//current controler
	var $p_c = '';//parent controller
  
  //set db - listing data
	var $e_list_limit 	= '10';
	var $e_list_offset 	= '0';
	var $e_list_order   = 'asc';
	var $e_list_order_by= 'os_journals_title'; 
  
  /*message strings*/
  var $valid_success 	= '';
  var $valid_errors 	= '';
  var $valid_notice 	= '';
  
  /**
   * journals  - constructor of the class
   * 
   * @access public
   * @param  NA
   * @return void
   */
	function journals()
	{
		parent::Controller();	
    
    $this->lang->load('common', ADMIN_LANGUAGE);
    $this->load->model('DB_F_Model', 'db_model');
	}
	
  /** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{
    $this->_clear_search();
		$this->client_list();
	}
  
  /** 
	 * Set Search
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _set_search()
	{
		$array_list = array();
    $array_list['srch_all_flds'] = $this->input->post("srch_all_flds");
    $array_list['srch_author'] = $this->input->post("srch_author");/*os_editor_name*/
    $array_list['srch_bk_title'] = $this->input->post("srch_bk_title");/*os_journals_title*/
    $array_list['srch_vol'] = $this->input->post("srch_vol");/*os_volume_no, os_volume_year*/
    $array_list['srch_issue'] = $this->input->post("srch_issue");/*os_volume_issue_no*/
    $array_list['srch_page'] = $this->input->post("srch_page");
    
    $this->session->set_userdata($this->c.'_search',$array_list);
	}
  
  /** 
	 * Get Seacrh
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _get_search($srch_name='',$all = FALSE)
	{
		$array_list = $this->session->userdata($this->c.'_search');
    if($all == TRUE){
      return $array_list;
    }else{
      return (isset($array_list[$srch_name]))?$array_list[$srch_name]:'';
    }    
	}
  
  /** 
	 * Clear Seacrh
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _clear_search()
	{
		$array_list = array();
    $array_list['srch_all_flds'] = '';
    $array_list['srch_author'] = '';
    $array_list['srch_bk_title'] = '';
    $array_list['srch_vol'] = '';
    $array_list['srch_issue'] = '';
    $array_list['srch_page'] = '';    
    $this->session->set_userdata($this->c.'_search',$array_list);
    $this->session->unset_userdata($this->c.'_search');
	}
  
  /** 
	 * Client List
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function client_list()
	{
		$data = array();
    
    $data = $this->db_model->fr_get_stats_data();
    
		$data['cont_title'] = 'New Releases';
		
		/*get all jounrals*/
    $this->e_list_limit = 25;
		$result_set 		= $this->_generate_list();
		$data['list'] 		= $result_set['list'];
		$data['num_rows'] 	= $result_set['num_rows'];    
    
    /*get all articles*/
		$result_set 		= $this->_generate_article_list();
		$data['art_list'] 		= $result_set['list'];
		$data['art_num_rows'] 	= $result_set['num_rows'];    
    
    $data['frm_name']   = 'frmjournallist';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		//__display('client_listing',$data);
    __display('blank',$data);
    
  }
	
	/** 
	 * List All
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function list_all()
	{
		$data = array();
		$data['cont_title'] = 'Journal Management System';
		
		//get all pages
		$result_set 		= $this->_generate_list();
		$data['list'] 		= $result_set['list'];
		$data['num_rows'] 	= $result_set['num_rows'];
		
    $data['frm_name']   = 'frmjournallist';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		__display('listing',$data);
	}
	
  
	/** 
	 * search
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function search()
	{
    if($_POST){
      $this->_set_search();
    }else{
      $this->_clear_search();
    }
    $this->searchlist();
  }
  
  /** 
	 * search list
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function searchlist()
	{
    $data = array();
		$data['cont_title'] = 'Search Results';
    
    /*set pagination items*/        
    $this->load->library('Digg_Pagination');
    $this->load->plugin('pagination');      
		
    /*get count and set pagination*/
    $result_cnt_set = $this->_generate_search_list(true);    
    $per_page_seg = 3;
    $cur_page_seg = 4;
    $per_page_val = get_per_page($this->e_list_limit, $per_page_seg);    
    $total_rows = $result_cnt_set['list']['cnt'];
    $this->e_list_offset = get_offset($cur_page_seg, $per_page_val);    
    $pbase_url = site_url($this->c .'/searchlist/');
    
    $pagination_link = init_paginate($cur_page_seg, $total_rows, $per_page_val, $per_page_seg, $pbase_url);
    $data['pagination_link']   = $pagination_link;
    $data['frm_name']   = 'frmjournalsrchlist';
    $data['frm_action'] = site_url( $this->c );
    
    //get all pages
		$result_set         = $this->_generate_search_list();
		$data['list']       = $result_set['list'];
		$data['num_rows'] 	= $result_cnt_set['list']['cnt'];
    
    $data['db_model'] = &$this->db_model;
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
    __display('search_listing',$data);
  }
  
  /** 
	 * Subjects
	 *
	 * @descp List all the pages in the system based on subjects
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function subjects($id = 0)
	{
    $id = (int) $id;
    $data = array();
    
		$data['cont_title'] = 'Subject - Journals';
     $qry = "SELECT * FROM ".TBL_CATEGORY." 
            WHERE `os_category_id` = ".$id;
    $cat_set = run_cust_qry($qry,TRUE);
    if($cat_set['num_rows'] > 0){
      $data['cont_title'] = 'Subject: '.$cat_set['list']['os_category_name'].''; 
    }		
        
    /*set pagination items*/        
    $this->load->library('Digg_Pagination');
    $this->load->plugin('pagination');      
		
    /*get count and set pagination*/
    $qry = "SELECT COUNT( DISTINCT(j.os_journals_id) ) as cnt 
            FROM ".TBL_JOURNALS." j
            LEFT OUTER JOIN ".TBL_JOR_MAP_CAT." cat ON cat.os_journals_id = j.os_journals_id 
            WHERE j.os_journals_status = '1' AND cat.os_category_id = '".$id."'";	
    //__debug($qry);exit(); 
		$result_cnt_set = run_cust_qry($qry,true);    
    $per_page_seg = 4;
    $cur_page_seg = 5;
    $per_page_val = get_per_page($this->e_list_limit, $per_page_seg);    
    $total_rows = $result_cnt_set['list']['cnt'];
    $this->e_list_offset = get_offset($cur_page_seg, $per_page_val);    
    $pbase_url = site_url($this->c .'/subjects/'.$id.'/');
    
    $pagination_link = init_paginate($cur_page_seg, $total_rows, $per_page_val, $per_page_seg, $pbase_url);
    $data['pagination_link']   = $pagination_link;
    
    $limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
    
		//get all pages
    $qry = "SELECT  DISTINCT(j.os_journals_id), j.*
            FROM ".TBL_JOURNALS." j
            LEFT OUTER JOIN ".TBL_JOR_MAP_CAT." cat ON cat.os_journals_id = j.os_journals_id 
            WHERE j.os_journals_status = '1' AND cat.os_category_id = '".$id."'
            ORDER BY j.os_journals_title $order
            LIMIT $offset , $limit";	
    //__debug($qry);exit(); 
		$result_set = run_cust_qry($qry);
		$data['list']       = $result_set['list'];
		$data['num_rows'] 	= $total_rows;
		
    $data['frm_name']   = 'frmjournalsibjects';
    $data['frm_action'] = site_url( $this->c );
    
    $data['db_model'] = &$this->db_model;
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
    __display('search_listing',$data);
  }
  
  
  /** 
	 * Book Preview
	 *
	 * @descp preview the PDF
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function bookpreview($id = 0)
	{
    $data = array();
    $id = (int) $id;
    if(empty($id)){
      redirect();
    }    
    
		$data['cont_title'] = 'Journal - Book Preview';	
    $data['jor_id'] = $id;
              
    /*get articles info*/
    $qry = "SELECT  a.*, j.os_journals_title, 
            j.os_journals_cover_img, j.os_journals_issn, j.os_journals_e_issn, j.os_journals_price 
            
            FROM ".TBL_ARTICLES." a
            JOIN ".TBL_JOURNALS." j
            ON a.os_journals_id = j.os_journals_id   
            WHERE os_articles_id = '".$id."'
            AND j.os_journals_status = '1'  
            ";		
    
		$articles_data = run_cust_qry($qry,TRUE);    
    $data['articles_data'] 	= $articles_data['list'];
    $data['journals_data'] 	= $articles_data['list'];
    $data['articles_num_rows'] 	= $articles_data['num_rows'];
    
    $data['frm_name']   = 'frm_contents_list';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
        
    $this->load->view('popupheader',$data);
    $this->load->view('bookpreview',$data);
    $this->load->view('popupfooter',$data);
  }
  
	/** 
	 * Contents List
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function contents_list($id = 0,$vol_id = 0)
	{
    $data = array();
    $temp_data = array();
    $id = (int) $id;
    $vol_id = (int) $vol_id;
    if(empty($id)){
      redirect();
    }    
    
		$data['cont_title'] = 'Journal Info';	
    $data['cur_jor_id'] = $id;
    $data['cur_vol_id'] = 0;
    
    /*get journal info*/    
    $journals_data = $this->db_model->fr_get_journal_data($id);
    if($journals_data['num_rows'] == 0){
      redirect();
    }    
    $data['journals_data'] 	= $journals_data['list'];
    $data['journals_num_rows'] 	= $journals_data['num_rows'];
        
    /*get all volume info - desc order*/
    $nav_volume_data = array();
    $volume_all_data = $this->db_model->fr_get_volume_tree($id);
    if($volume_all_data['num_rows'] > 0){
      foreach($volume_all_data['list'] as $key => $val){
        $yr = (!empty($tmp_vol['os_volume_issue_dt_range']))? ' - '.$tmp_vol['os_volume_issue_dt_range']:'';
        $id_str = 'Volume '.$val['os_volume_no'].$yr;
        $nav_volume_data[$id_str]['set'] = $id_str ;  
        $nav_volume_data[$id_str]['val'][] = $val;
      }
    }
    $data['nav_volume_data'] 	= $nav_volume_data;
    $data['nav_volume_num_rows'] 	= $volume_all_data['num_rows'];
    
    /*get latest volume info*/    
    $current_volume = $this->db_model->fr_get_cur_volume($id,$vol_id);
    $data['current_volume'] = $current_volume['list'];
    
    /*get articles for the volume and issue data*/
    $cur_vol_id = (isset($current_volume['list']['os_volume_id'])) ? $current_volume['list']['os_volume_id']:0;
    $cur_articles_data = $this->db_model->fr_get_cur_articles($id,$cur_vol_id);
       
    $data['cur_vol_no']     = (isset($cur_articles_data['cur_vol_no'])) ? $cur_articles_data['cur_vol_no']:'-';;
    $data['cur_issue_no']   = (isset($cur_articles_data['cur_issue_no'])) ? $cur_articles_data['cur_issue_no']:'-';;
    $data['cur_vol_pages']  = (isset($cur_articles_data['cur_vol_pages'])) ? $cur_articles_data['cur_vol_pages']:'-';
    
    /*get all articles list*/
    $articles_list = $this->db_model->fr_get_articles_list($id,$cur_vol_id);
    $data['articles_list'] = $articles_list;
    
    /*get volume info*/
    /*$qry = "SELECT  * 
            FROM ".TBL_VOLUME."
            WHERE os_journals_id = '".$id."'            
            ORDER BY os_volume_id
            LIMIT 1
            ";			
		$volume_data = run_cust_qry($qry,TRUE);    
    $data['volume_data'] 	= $volume_data['list'];
    $data['volume_num_rows'] 	= $volume_data['num_rows'];*/
    
    
    /*get publisher info*/    
    $str_pub_name = $this->db_model->fr_get_pub_name($id);
    $data['str_pub_name']   = $str_pub_name;
    $str_pub_id = $this->db_model->fr_get_pub_name($id, TRUE);
    $data['str_pub_id']   = $str_pub_id;
    
    /*get category info*/    
    $cat_info = $this->db_model->fr_get_cat_name($id);
    $data['cat_info']   = $cat_info;
              
    $data['frm_name']   = 'frm_contents_list';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
        
    __display('contents_list',$data);
  }
     
  /** 
 	 * General List
	 *
	 * @descp NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _generate_list()
	{
		$return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
		
		$qry = "SELECT  * 
            FROM ".TBL_JOURNALS."
            WHERE os_journals_status = '1'
            ORDER BY  $order_by	$order
            LIMIT $offset  , $limit 							  			
            ";			
		$return_arr = run_cust_qry($qry);
		return $return_arr;
	}
  
  /** 
 	 * General Article List
	 *
	 * @descp NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _generate_article_list()
	{
		$return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
		
		$qry = "SELECT  * 
            FROM ".TBL_ARTICLES."
            WHERE os_articles_status = '1'
            ORDER BY  os_articles_title	$order
            LIMIT $offset  , $limit 							  			
            ";			
		$return_arr = run_cust_qry($qry);
		return $return_arr;
	}
  
  /** 
 	 * Generate Editors for Listing
	 *
	 * @descp NA
	 * @access public/private
	 * @param  array
	 * @return void/mixed
	 */
	function _generate_editors_listing($dataset=array())
	{
		$return_arr  = array();
    $ids = array();
		if($dataset['num_rows'] > 0){
      foreach($dataset['list'] as $val){
        $ids[] = $val['os_journals_id'];
      }
      
      $qry = "SELECT  e.os_editor_name as nme, m.os_journals_id as id
              FROM ".TBL_EDITOR." e
              JOIN ".TBL_JOR_MAP_EDITOR." m
              ON m.os_editor_id = e.os_editor_id  
              WHERE m.os_journals_id IN (".  implode(', ', $ids).")
              ORDER BY os_editor_name
              ";			
      $handle = $this->db->query($qry);
      //__debug($qry);
      $num_rows = $handle->num_rows();
      $var['num_rows'] = $num_rows;
      if ($num_rows > 0) {
        $result_set = $handle->result_array();
        foreach ($result_set as $row) {
          $return_arr[$row['id']][] = $row['nme'];
        }
      }
    }
    
		return $return_arr;
	}
  
  /** 
 	 * Generate Search List
	 *
	 * @descp NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _generate_search_list($cnt = FALSE)
	{
		$return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
    
    $srch_all_flds    = $this->_get_search("srch_all_flds");
    $srch_author      = $this->_get_search("srch_author");/*os_editor_name*/
    $srch_bk_title    = $this->_get_search("srch_bk_title");/*os_journals_title*/
    $srch_vol         = $this->_get_search("srch_vol");/*os_volume_no, os_volume_year*/
    $srch_issue       = $this->_get_search("srch_issue");/*os_volume_issue_no*/
    $srch_page        = $this->_get_search("srch_page");/*os_volume_pages*/
		
    $search_str = "";
    
    if(!empty($srch_all_flds)){
      $search_str .= " AND 
                        (
                          j.os_journals_editorial ".filter_data($srch_all_flds,'search_like')." 
                          OR
                          j.os_journals_title ".filter_data($srch_all_flds,'search_like')." 
                          OR
                          j.os_journals_abstract ".filter_data($srch_all_flds,'search_like')." 
                          OR
                          j.os_journals_issn ".filter_data($srch_all_flds,'search_like')." 
                          OR
                          vol.os_volume_no = '". $this->db->escape_like_str(filter_data($srch_all_flds))."'
                          OR
                          vol.os_volume_issue_no = '". $this->db->escape_like_str(filter_data($srch_all_flds))."'
                          OR
                          art.os_articles_title ".filter_data($srch_all_flds,'search_like')."   
                          OR
                          art.os_articles_abstract ".filter_data($srch_all_flds,'search_like')."                               
                          ) ";
    }
    if(!empty($srch_author)){      
      $search_str .= " AND j.os_journals_editorial ".filter_data($srch_author,'search_like')." ";
    }
    if(!empty($srch_bk_title)){
      $search_str .= " AND j.os_journals_title ".filter_data($srch_bk_title,'search_like')." ";
    }
    if(!empty($srch_vol)){
      $search_str .= " AND ( 
                        vol.os_volume_no = '". $this->db->escape_like_str(filter_data($srch_vol))."'
                        OR
                        vol.os_volume_year = '". $this->db->escape_like_str(filter_data($srch_vol))."'
                       ) ";
    }
    if(!empty($srch_issue)){
      $search_str .= " AND ( 
                        vol.os_volume_issue_no = '". $this->db->escape_like_str(filter_data($srch_issue))."'                        
                       ) ";
    }
    if(!empty($srch_page)){
      $search_str .= " AND ( art.os_articles_pages ".filter_data($srch_page,'search_like')." ) ";
    }
    
    $select_flds = '';
    if($cnt == TRUE){
      $select_flds = ' COUNT(DISTINCT(art.os_articles_id)) as cnt ';
    }else{
      $select_flds = ' DISTINCT(art.os_articles_id),
                        j.*,
                        art.os_articles_id,
                        art.os_articles_title,
                        art.os_articles_pdf,
                        art.os_articles_pages,
                        art.os_articles_author,
                        art.os_articles_abstract,
                        art.os_articles_acess_type ';
    }
    
		$qry = "SELECT  ".$select_flds."            
            FROM ".TBL_JOURNALS." j            
            LEFT OUTER JOIN ".TBL_JOR_MAP_EDITOR." me ON me.os_journals_id = j.os_journals_id            
            LEFT OUTER JOIN ".TBL_EDITOR." ed ON me.os_editor_id = ed.os_editor_id            
            LEFT OUTER JOIN ".TBL_VOLUME." vol ON vol.os_journals_id = j.os_journals_id             
            LEFT OUTER JOIN ".TBL_ARTICLES." art ON art.os_volume_id = vol.os_volume_id  
            WHERE            
            j.os_journals_status = '1'
            AND art.os_articles_status = '1'
            $search_str
            
            ";			
    
   if($cnt != TRUE){
      $qry .= " ORDER BY j.os_journals_title $order
                LIMIT $offset , $limit 	";
    }
    
    //__debug('<hr>'.$qry);
    ////exit();
    if($cnt == TRUE){
      $return_arr = run_cust_qry($qry,TRUE);
    }else{
      $return_arr = run_cust_qry($qry);
    }
		return $return_arr;
	}    
  
  
  function previewpdf($id){
    $data = array();
    $id = (int) $id;
    if(empty($id)){
      redirect();
    }    
    
		$data['cont_title'] = 'Journal - Book Preview';	
    $data['jor_id'] = $id;
              
    /*get articles info*/
    $qry = "SELECT  a.*, j.os_journals_title, 
            j.os_journals_cover_img, j.os_journals_issn, j.os_journals_e_issn, j.os_journals_price 
            
            FROM ".TBL_ARTICLES." a
            JOIN ".TBL_JOURNALS." j
            ON a.os_journals_id = j.os_journals_id   
            WHERE os_articles_id = '".$id."'
            AND j.os_journals_status = '1'  
            ";		
    
		$articles_data = run_cust_qry($qry, TRUE);
    if($articles_data['num_rows'] > 0){
      if(__is_loggedin()){
        $art_info = _articles_file($articles_data['list']['os_articles_pdf'], $articles_data['list']['os_articles_id']);    
        if(!empty($art_info['path'])){ 
          $mime_type = 'application/pdf';
          $name = time() .'_'. $articles_data['list']['os_articles_pdf'];
          $file = $art_info['path'];
          if (!is_readable($file))
            die('File not found or inaccessible! Please try again later.');

          $size = filesize($file);
          $name = rawurldecode($name);
          
          /*http://www.php.net/manual/en/features.http-auth.php
           * if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}*/  
          
    


header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);
exit;      
          // Headers
          header("Content-Type: application/octet-stream; ");
          header("Content-Transfer-Encoding: binary");
          header("Content-Length: " . filesize($file) ."; ");
          header("filename=\"".$file."\"; ");
          flush(); // this doesn't really matter.

          // Opening the file in read-only
          $fp = fopen($file, "r");
          while (!feof($fp))
          {
              // Read a buffer size of the file
              $buffer = fread($fp, $bufferlength);
              $j=0;
              for ($i=0; $i < $bufferlength; $i++) {
                  // The key is read in loop to crypt the whole file
                  if ($i%$keylength == 0) {
                      $j=0;
                  }
                  // Apply a xor operation between the key and the file to crypt
                  // This operation eats a lots of CPU time (Stream at 1MiB/s on my server; Intel E2180)
                  $tmp = pack("C", $key[$j]);
                  $bufferE = ( $buffer[$i]^$tmp); // <==== Le fameux XOR

                  /*
                  echo "<br />key[".$j."]: ";
                  var_dump($tmp);
                  echo "<br />buffer[".$i."]: ";
                  var_dump($buffer[$i]);
                  echo "<br />bufferE: ";
                  var_dump($bufferE);
                  echo "<br />";
                  //*/

                  // Send the encrypted data
                  echo $bufferE;
                  // Clean the memory
                  $bufferE = "";
                  $j++;
              }
              $buffer = "";
              flush(); // this is essential for large downloads
              /*
              fclose($fp);
              exit();
              //*/
          }
          // Close the file and it's finished
          fclose($fp); 
          
          exit;       
          }    
        }else{
          __e("Sorry you are not authorised to view this file. Please Login");
        } 
    }else{
      __e(LIST_NO_DATA_MSG);
    }
  }
  
  /** 
	 * List All
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function maxdownloads()
	{
    if(!__is_loggedin()){
      redirect();
    }
    
		$data = array();
		$data['cont_title'] = 'Journal/Articles Downloads';
		
		$data['frm_name']   = 'frmjournallist';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		__display('maxdownloads',$data);
	}
}
/* End of file journals.php */
/* Location: ./system/application/modules/journals/controllers/journals.php */